package com.lanhe.app.controller.member;

import cn.hutool.core.bean.BeanUtil;
import com.lanhe.common.core.controller.BaseController;
import com.lanhe.common.core.domain.AjaxResult;
import com.lanhe.common.core.domain.entity.LhMemberBase;
import com.lanhe.common.core.domain.model.LoginUser;
import com.lanhe.common.core.page.TableDataInfo;
import com.lanhe.common.utils.StringUtils;
import com.lanhe.common.utils.regex.RegExUtil;
import com.lanhe.member.domain.dto.LhMemberBaseDTO;
import com.lanhe.member.domain.vo.LhMemberBaseVO;
import com.lanhe.member.service.ILhMemberBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 会员基础表(LhMemberBase)表控制层
 *
 * @author zhanggl
 * @since 2023-12-04 17:01:12
 */
@RestController
@RequestMapping("/api/member")
public class ApiMemberBaseController extends BaseController {

	@Autowired
	private ILhMemberBaseService lhMemberBaseService;

	/**
	 * @Description 用户手机号是否存在
	 * @MethodName mobileIsExist
	 * @Param []
	 * @ReturnType com.lanhe.common.core.domain.AjaxResult
	 * @Date 2023/12/6 18:43
	 * @Author zxx
	 * @Since 1.0
	 */
	@PostMapping("mobileIsExist")
	public AjaxResult mobileIsExist() throws Exception {
		LoginUser loginUser = getLoginUser();
		if (Objects.isNull(loginUser)) {
			return error("授权失败");
		}
		LhMemberBase lhMemberBase = loginUser.getLhMemberBase();

		LhMemberBaseVO lhMemberBaseDetail = lhMemberBaseService.getLhMemberBaseDetail(lhMemberBase.getMemberId());

		if (StringUtils.isBlank(lhMemberBaseDetail.getMobile())) {
			return success(1);
		} else {
			return success(0);
		}
	}

	/**
	 * @Description 更新用户信息
	 * @MethodName updateMemberBase
	 * @ReturnType com.lanhe.common.core.domain.AjaxResult
	 * @Date 2023/12/6 18:46
	 * @Author zxx
	 * @Since 1.0
	 */
	@PostMapping("/updateMemberBase")
	public AjaxResult updateMemberBase(@RequestBody LhMemberBaseDTO dto) throws Exception {
		LoginUser loginUser = getLoginUser();
		if (Objects.isNull(loginUser)) {
			return error("授权失败");
		}
		LhMemberBase memberBaseVo = new LhMemberBase();
		BeanUtil.copyProperties(dto, memberBaseVo);
		memberBaseVo.setMemberId(getUserId());
		memberBaseVo.setUpdateTime(new Date());
		LhMemberBase memberBase = lhMemberBaseService.updateLhMemberBase(memberBaseVo);
		return success(memberBase);
	}


	/**
	 * 获取用户信息
	 *
	 * @author zhanggl
	 * @date 2023/12/6
	 */
	@GetMapping("getUserInfo")
	public AjaxResult getUserInfo() throws Exception {
		LhMemberBaseVO lhMemberBaseDetail = lhMemberBaseService.getLhMemberBaseDetail(getUserId());
		return success(lhMemberBaseDetail);
	}

	/**
	 * 绑定手机号
	 *
	 * @return com.lanhe.common.core.domain.AjaxResult
	 * @author zhanggl
	 * @date 2023/12/6
	 */
	@PostMapping("/bindMobile")
	public AjaxResult bindMobile(@RequestBody LhMemberBaseDTO dto) {
		dto.setMemberId(getUserId());
		// 1.校验手机号格式
		if (!RegExUtil.matchesMobile(dto.getMobile())) {
			return error("手机号格式不正确");
		}
		if (StringUtils.isBlank(dto.getSmsCode())) {
			return error("手机验证码不能为空");
		}
		return success(lhMemberBaseService.bindMobile(dto));
	}

	/**
	 * 排行榜
	 *
	 * @author zhanggl
	 * @date 2023/12/6
	 */
	@GetMapping("/rankingList")
	public TableDataInfo rankingList(@RequestParam(name = "type") Integer type,
	                                 @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
	                                 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {

		startPage();
		List<Object> list = lhMemberBaseService.rankingList(type,pageNum,pageSize);
		return getDataTable(list);
	}
}

